package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-11 22:25
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.AccessNetwork;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.UserAddressMapper;
import com.datang.pet.mapper.UserInfoMapper;
import com.datang.pet.mapperExtend.UserInfoMapperExtend;
import com.datang.pet.pojo.UserAddress;
import com.datang.pet.pojo.UserInfo;
import com.datang.pet.pojo.UserInfoExample;
import com.datang.pet.service.UserService;
import com.datang.pet.vo.ReadUserInfo;
import com.datang.pet.vo.UpdateUserInfoAndAddUserAddressRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private UserInfoMapperExtend userInfoMapperExtend;


    /*
     * @author 顶风少年
     * @date 2019/6/11 22:59
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param id
     * @param code
     * @result UserInfo
     * @notify 用户不存在注册用户存在返回用户
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public UserInfo getUserIfNullAddUser(String id, String code) throws Exception {

        //传进来的userId不为空
        if (StringUtils.isNUll(id)) {
            //md5解密userId
            try {
                id = MD5.decryptData(id);
            } catch (Exception e) {
                throw new Exception("userId解密出错！");
            }

            //查询是否UserId是否存在
            try {
                UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
                //设置明文id
                userInfo.setPlainText(userInfo.getId());
                //md5加密userId
                userInfo.setId(MD5.encryptData(userInfo.getId()));
                //隐藏openId
                userInfo.setOpenid("");

                return userInfo;
            } catch (Exception e) {
                throw new Exception("用户不存在");
            }
        } else {
            //微信唯一标识
            String openId = null;
            try {
                //调用访问互联网的工具类
                AccessNetwork wxCode = new AccessNetwork();
                //获取openId的方法
                openId = wxCode.getUserOpenId(code);
            } catch (Exception e) {
                throw new Exception("获取openid失败");
            }


            List<UserInfo> user = new ArrayList<>();
            try {
                //使用openId查询用户的userId
                UserInfoExample userInfoExample = new UserInfoExample();
                UserInfoExample.Criteria criteria = userInfoExample.createCriteria();
                criteria.andOpenidEqualTo(openId);
                //查询openId是否存在
                user = userInfoMapper.selectByExample(userInfoExample);
            } catch (Exception e) {
                throw new Exception("查询用户失败");
            }


            //如果openId不存在
            if (user.isEmpty()) {
                //添加用户数据
                try {
                    //openId不存在,注册用户
                    UserInfo userInfo = new UserInfo();
                    userInfo.setId(StringUtils.getId());
                    userInfo.setOpenid(openId);
                    userInfo.setMobileTelephone("");
                    userInfo.setGrade(0);
                    userInfo.setNikeName("");
                    userInfo.setHeadPortrait("");
                    userInfo.setBirthday(null);
                    userInfo.setGender("1");
                    userInfo.setQq("");
                    userInfo.setWechat("");
                    userInfo.setRegisterDate(new Date());
                    userInfo.setFansQuantity(0);
                    userInfo.setConcernQuantity(0);
                    userInfo.setLikeQuantity(0);
                    userInfo.setPetQuantity(0);
                    userInfo.setCoinQuantity(0);
                    userInfoMapper.insertSelective(userInfo);
                    //md5加密userId
                    //设置明文id
                    userInfo.setPlainText(userInfo.getId());
                    userInfo.setId(MD5.encryptData(userInfo.getId()));
                    //隐藏openId
                    userInfo.setOpenid("");
                    //如果添加成功返回用户信息
                    return userInfo;
                } catch (Exception e) {
                    throw new Exception("添加用户数据时出错");
                }
            } else {
                //返回用户信息
                UserInfo userInfo = user.get(0);
                //设置明文id
                userInfo.setPlainText(userInfo.getId());
                //md5加密userId
                userInfo.setId(MD5.encryptData(userInfo.getId()));
                //隐藏openId
                userInfo.setOpenid("");
                return userInfo;
            }
        }
    }


    /*
     * @author 顶风少年
     * @date 2019/6/11 23:00
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param updateUserInfoAndAddUserAddressRequest
     * @result void
     * @notify 修改用户头像昵称和地理位置
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public UserInfo UpdateUserInfoAndAddUserAddress(UpdateUserInfoAndAddUserAddressRequest updateUserInfoAndAddUserAddressRequest) throws Exception {

        String userId = null;
        try {
            userId = MD5.decryptData(updateUserInfoAndAddUserAddressRequest.getId());
        } catch (Exception e) {
            throw new Exception("解密用戶id失敗");
        }

        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(userId);
            userInfo.setCity(updateUserInfoAndAddUserAddressRequest.getCity());
            userInfo.setBirthday(updateUserInfoAndAddUserAddressRequest.getBirthday());
            userInfo.setProvince(updateUserInfoAndAddUserAddressRequest.getProvince());
            userInfo.setDistrict(updateUserInfoAndAddUserAddressRequest.getDistrict());
            userInfo.setGender(updateUserInfoAndAddUserAddressRequest.getGender());
            userInfo.setHeadPortrait(updateUserInfoAndAddUserAddressRequest.getHeadPortrait());
            userInfo.setNikeName(updateUserInfoAndAddUserAddressRequest.getNikeName());
            userInfoMapper.updateByPrimaryKeySelective(userInfo);
        } catch (Exception e) {
            throw new Exception("更改用户信息出错");
        }


//        //添加登录记录
//        try {
//            UserAddress userAddress = new UserAddress();
//            userAddress.setId(StringUtils.getId());
//            userAddress.setUserId(userId);
//            userAddress.setProvince(updateUserInfoAndAddUserAddressRequest.getProvince());
//            userAddress.setCity(updateUserInfoAndAddUserAddressRequest.getCity());
//            userAddress.setDistrict(updateUserInfoAndAddUserAddressRequest.getDistrict());
//            userAddress.setAddress(updateUserInfoAndAddUserAddressRequest.getAddress());
//            userAddress.setTime(new Date());
//            userAddressMapper.insertSelective(userAddress);
//        } catch (Exception e) {
//            throw new Exception("添加用户登录记录失败");
//        }
        try {
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
            //设置明文id
            userInfo.setPlainText(userInfo.getId());
            //md5加密userId
            userInfo.setId(MD5.encryptData(userInfo.getId()));
            //隐藏openId
            userInfo.setOpenid("");
            return userInfo;

        } catch (Exception e) {
            throw new Exception("查询用户信息失败");
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/6/7 23:12
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result boolean
     * @notify 查询用户是否存在
     * version 1.0
     */
    public boolean userIsExist(String userId) throws Exception {

        try {
            userId = MD5.decryptData(userId);
        } catch (Exception e) {
            throw new Exception("解密用戶id失敗");
        }
        UserInfo userInfo = null;
        try {
            userInfo = userInfoMapper.selectByPrimaryKey(userId);
        } catch (Exception e) {
            throw new Exception("查询用户出错");
        }
        if (userInfo == null) {
            return false;
        } else {
            return true;
        }
    }

    public ReadUserInfo readUserInfo(String userId) throws Exception{

        return userInfoMapperExtend.readUserInfo(userId);

//        try {
//          return   userInfoMapperExtend.readUserInfo(userId);
//        }catch (Exception e){
//            throw new Exception("查看用户信息异常");
//        }

    }
}
